<?php

class Picks_Model extends Model {

    function __construct() {
        parent::__construct();
    }
    
    /**
     * Return info for Rounds (nr, roundStatus)
     * @return type PDO-statement. 
     * data = returnvalue->fetchColumn(); 
     * output = data['columnname'];
     */
    function GetRounds() {
        $sth = $this->database->prepare("SELECT Rounds.nr, Rounds.roundStatus FROM Rounds ORDER BY nr ASC");
        $sth->execute();
        return $sth;
    }
    
    function GetMatchsInRound($RoundNr) {
        $sth = $this->database->prepare("SELECT COUNT(*) FROM Matchs WHERE :RoundNr=Matchs.round");
        $sth->execute(array(
            ':RoundNr' => $RoundNr,
        ));
        
        $data = $sth->fetch();
        return $data[0];
    }

    /**
     * Return info for RoundNr
     * @return type PDO-statement. 
     * data = returnvalue->fetchColumn(); 
     * output = data['columnname'];
     */
    function GetRoundInfo($RoundNr) {
        $sth = $this->database->prepare("SELECT Rounds.nr, Rounds.roundStatus FROM Rounds WHERE :RoundNr=Rounds.nr");
        $sth->execute(array(
            ':RoundNr' => $RoundNr,
        ));
        return $sth;
    }

    /**
     * Return info for 'points', 'priority', 'pickId' for a given memberId and RoundNr
     * @param type $RoundNr
     * @param type $memberId 
     * data = returnvalue->fetchColumn(); 
     * output = data['columnname'];
     * 
     */
    function GetPriorityPoints($RoundNr, $memberId) {
        $sthPriority = $this->database->prepare("SELECT PriorityPoints.points, PriorityPoints.priority, Picks.pickId, Picks.picksResult, Matchs.matchResult FROM `Matchs` INNER JOIN Picks ON Matchs.matchID = Picks.matchId  INNER JOIN PriorityPoints ON PriorityPoints.priority = Picks.priority WHERE Matchs.round =" . $RoundNr . " AND Picks.memberId =" . $memberId . " ORDER BY Matchs.defaultPriority");
        $sthPriority->execute();
        return $sthPriority;
    }
        
    function GetPicksStatus($picksResult, $matchResult)
    {
        //Match is not played or pick is net set, neutral color
        if(strcmp($picksResult,'Not Played')==0 || strcmp($matchResult,'Not Played')== 0)
            return 0;
        
        //Match is played tie or match result and picks result is the same
        if(strcmp($matchResult,'Tie')==0 || strcmp($picksResult, $matchResult)==0)
            return 1;
        else
            return -1;        
    }
    
    function CheckRoundOpen($Round) {
        $sthRoundstatus = $this->database->prepare("SELECT roundStatus FROM Rounds WHERE nr = :nr");
        $sthRoundstatus->execute(array(
            ':nr' => $Round,
        ));
        
        $RoundStatus = $sthRoundstatus->fetchColumn();

        
        if(!strcmp($RoundStatus, 'SetPicks') == 0) //Not possible to change picks
                return false;
        
        return true;

    }

    /**
     * Set priority for a given $Round and memberId 
     * @param type $PriorityArray , list with prioritys, index after default match priority
     * @param type $Round, Round nr
     * @param type $memberId 
     * 
     */
    function SetPriorityPointsForRound($PriorityArray, $Round, $memberId) {

        if(!$this->CheckRoundOpen($Round))
                return;
        
        //Get Default priority list
        $sthDefaultPriorityList = $this->database->prepare("SELECT pickId FROM Picks JOIN Matchs ON Picks.matchId=Matchs.matchId WHERE memberId=:memberId AND Matchs.round=:round ORDER BY Matchs.defaultPriority ASC");
        $sthDefaultPriorityList->execute(array(
            ':memberId' => $memberId,
            ':round' => $Round,
        ));

        $PriorityIndex = 0;
        while ($data = $sthDefaultPriorityList->fetch()) {
            $PickId = $data['pickId'];
            $Priority = $PriorityArray[$PriorityIndex];
            $PriorityIndex++;
            
            $sthUpdatePriority = $this->database->prepare("UPDATE Picks SET priority = :Priority WHERE pickId = :PickId");
            $sthUpdatePriority->execute(array(
                ':Priority' => $Priority,
                ':PickId' => $PickId,
            ));
        }
    }

    /**
     * Return info for 'picksResult', 'nameTeamA', displayNameTeamA, 'imageNameTeamA', 'nameTeamB', displayNameTeamA, 'imageNameTeamB', 'matchResult'
     * @param type $RoundNr
     * @param type $memberId 
     * data = returnvalue->fetchColumn(); 
     * output = data['columnname'];
     * 
     */        
    function GetPicksInfo($RoundNr, $memberId) {
        //Get Matchs in default priority        
        $sthMatchs = $this->database->prepare("SELECT Picks.picksResult, Picks.pickId, Matchs.nameTeamA, TeamA.ImageName AS imageNameTeamA, TeamA.displayName AS displayNameTeamA, TeamB.displayName AS displayNameTeamB, Matchs.nameTeamB, TeamB.ImageName AS imageNameTeamB, Matchs.matchResult FROM Matchs INNER JOIN Picks ON Matchs.matchId=Picks.matchId JOIN Teams AS TeamA ON Matchs.nameTeamA=TeamA.name JOIN Teams AS TeamB ON Matchs.nameTeamB=TeamB.name WHERE Matchs.round=" . $RoundNr . " AND Picks.memberId=" . $memberId);
        $sthMatchs->execute();
        return $sthMatchs;
    }
    
    function SetPicks($roundNr, $PickId, $memberId, $Result) {
        if(!$this->CheckRoundOpen($roundNr))
                return;
        
        $sthUpdatePriority = $this->database->prepare("UPDATE Picks SET picksResult = :result WHERE pickId = :PickId AND memberId=:memberId");
            $sthUpdatePriority->execute(array(
                ':result' => $Result,
                ':PickId' => $PickId,
                ':memberId' => $memberId,
            ));
    }

    function xhrgetmessages($NrOfMessages) {
 
        $sthMessageIndex = $this->database->prepare("SELECT Messages.message, Messages.dateTime, Members.username FROM Messages INNER JOIN Members ON Messages.memberId=Members.memberId ORDER BY Messages.dateTime DESC LIMIT ".$NrOfMessages);
        $sthMessageIndex->setFetchMode(PDO::FETCH_ASSOC);
        $sthMessageIndex->execute();
        $data = $sthMessageIndex->fetchAll();
        echo json_encode($data);
    }
    
    function AddMessage($memberId, $message) {
        $sthAddMessage = $this->database->prepare("INSERT INTO Messages (memberId, message, dateTime) VALUES(:memberId, :message, NOW())");
        $sthAddMessage->execute(array(
            ':memberId' => $memberId,
            ':message' => $message
        ));        
    }
    
    function xhrsendmessage() {

        $this->AddMessage($_SESSION['memberId'], $_POST['text']);
        $data = array(
            'text' => $_POST['text'],
            'username' => $_SESSION['username']
        );
        echo json_encode($data);
    }
    
    
    function SetPicksRound($RoundNrString) {
        
        $RoundNr = explode('_', $RoundNrString);
        $RoundNr = $RoundNr[1];
        $sth = $this->GetPicksInfo($RoundNr, $_SESSION['memberId']); 
        //Echo text to put in HTML  
         while ($data = $sth->fetch()) {

            $PicksResult = $data['picksResult'];
            $NameTeamA = $data['nameTeamA'];
            $DisplayNameTeamA = $data['displayNameTeamA'];
            $ImageTeamA = $data['imageNameTeamA'];
            $NameTeamB = $data['nameTeamB'];
            $DisplayNameTeamB = $data['displayNameTeamB'];
            $ImageTeamB = $data['imageNameTeamB'];
            $pickId = $data['pickId'];
 
            echo '<div class="ui-grid-d">'.PHP_EOL;
            echo '  <div class="ui-block-a" style="text-align: center;"><p>100</p></div>'.PHP_EOL;
            echo '  <div class="ui-block-b" style="background-color:#FF0000; text-align: center;"><p>'.$DisplayNameTeamA.'</p></div>'.PHP_EOL;
            echo '  <div class="ui-block-c" style="text-align: center;"><p>VS</p></div>'.PHP_EOL;
            echo '  <div class="ui-block-d" style="background-color:#00FF00; text-align: center;"><p>'.$DisplayNameTeamB.'</p></div>'.PHP_EOL;
            echo '  <div class="ui-block-e" style="text-align: center;"> <a href="index.html">Edit</a> </div>'.PHP_EOL;  
            echo '</div>'.PHP_EOL;  
        }
 
        
    }
            
   function SetSelectMatch($PickIDString) {
        
        $PickID = explode('_', $PickIDString);
        $PickID = $PickID[1];
        echo '  <option value="PickId">'.'PickID '.$PickID.'</option>' . PHP_EOL;
 /*     
        $sth = $this->GetRoundInfo($RoundNr);
        $data = $sth->fetch();
                
        $RoundStatus = $data['roundStatus'];
        
 //       echo '<div>Round status: ' . $RoundStatus . '</div>' . PHP_EOL;
        
        $sth = $this->GetPicksInfo($RoundNr, $_SESSION['memberId']);

        //Echo text to put in HTML         
 //       echo '<select name="SelectMatchNr" id="SelectMatchNr_1">' . PHP_EOL;
        
         while ($data = $sth->fetch()) {

            $PicksResult = $data['picksResult'];
            $NameTeamA = $data['nameTeamA'];
            $DisplayNameTeamA = $data['displayNameTeamA'];
            $ImageTeamA = $data['imageNameTeamA'];
            $NameTeamB = $data['nameTeamB'];
            $DisplayNameTeamB = $data['displayNameTeamB'];
            $ImageTeamB = $data['imageNameTeamB'];
            $pickId = $data['pickId'];
 
            echo '  <option value="PickId_'.$pickId.'">'.$DisplayNameTeamA.' VS '.$DisplayNameTeamB.'</option>' . PHP_EOL;
        }
   //     echo '</select>' . PHP_EOL;
*/
     }
  
    
    
}

?>